package instancecreate

import (
	"errors"
	"fmt"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/setting"
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/util"
	"log"
)

func CreateNetwork(networkName string) error {
	nS := fmt.Sprintf(
		"if ! sudo docker network list | grep %s; then sudo docker network create -d overlay --attachable %s ;fi",
		networkName,
		networkName,
	)

	sshClient, err := util.NewSshClient(setting.DockerManagerHost, setting.DockerManagerPort,
		setting.DockerManagerUser, setting.DockerManagerAuthKey)
	if err != nil {
		log.Println(err.Error())
		return err
	}

	errMsg, err := util.RunSshCommand(sshClient, nS)
	if err != nil {
		log.Println(err.Error(), errMsg)
		return errors.New(errMsg)
	}
	return nil
}
func DeleteNetwork(networkName string) error {
	nS := fmt.Sprintf(
		"if sudo docker network list | grep %s; then sudo docker network rm %s ;fi",
		networkName,
		networkName,
	)

	sshClient, err := util.NewSshClient(setting.DockerManagerHost, setting.DockerManagerPort,
		setting.DockerManagerUser, setting.DockerManagerAuthKey)
	if err != nil {
		log.Println(err.Error())
		return err
	}

	errMsg, err := util.RunSshCommand(sshClient, nS)
	if err != nil {
		log.Println(err.Error(), errMsg)
		return errors.New(errMsg)
	}
	return nil
}
